﻿using System.Collections.Generic;
using System.Web.UI.WebControls;
using Inkubator.Wizard.Model.Model;

namespace Inkubator.Wizard.WizardGenerator.Generator
{
    public abstract class WebControlFactory
    {

        public enum ControlType
        {
            Text, Group, Date, Table
        }

        /// <summary>
        /// This method return a WebControl based on the properties of the objects received as arguments.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="component"></param>
        /// <returns></returns>
        public static WebControl webControlFactory(ControlType type, WizardComponent component)
        {
            switch (type)
            {
                case ControlType.Text:
                    TextBox text = new TextBox();
                    if (((Text)component).password)
                    {
                        text.TextMode = TextBoxMode.Password;
                    }
                    return text;
                case ControlType.Date:
                    return new Calendar();
                case ControlType.Group:
                    {
                        Group group = (Group)component;

                        ListControl groupControl = null;
                        switch (group.type)
                        {
                            case "list":
                                groupControl = new ListBox(); break;
                            case "check":
                                groupControl = new CheckBoxList(); break;
                            case "radio":
                                groupControl = new RadioButtonList(); break;
                            case "dropdown":
                                groupControl = new DropDownList(); break;
                        }

                        List<string> list = group.item;
                        groupControl.DataSource = list;
                        groupControl.DataBind();

                        return groupControl;
                    }
                case ControlType.Table:
                    {
                        TableData tableData = (TableData)component;

                        Table table = new Table();
                        table.Caption = tableData.label;

                        for (int j = 0; j < tableData.rows; j++)
                        {
                            TableRow row = new TableRow();
                            table.Rows.Add(row);

                            for (int q = 0; q < tableData.columns; q++)
                            {
                                TableCell cell = new TableCell();
                                cell.Controls.Add(new TextBox());
                                row.Cells.Add(cell);
                            }
                        }

                        table.BorderStyle = BorderStyle.Solid;
                        table.CellSpacing = 5;
                        table.GridLines = GridLines.Both;
                        table.BorderWidth = 2;

                        return table;
                    }

            }

            throw new System.NotSupportedException("Not Valid Type");
        }

    }
}
